import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { Router } from '@angular/router'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage'
import { Modal } from 'ng-zorro-antd-mobile'

@Component({
  selector: 'app-setting',
  templateUrl: './setting.component.html',
  styleUrls: ['./setting.component.scss'],
  providers: [Modal]
})
export class SettingComponent implements OnInit {
  // 是否登录
  login = false
  appVersion: string

  constructor(
    private router: Router,
    private location: Location,
    private utils: CommonUtil,
    private storage: CoolLocalStorage,
    private modal: Modal
  ) {}

  // 初始化
  ngOnInit() {
    let token = this.storage.getItem('token')
    let appVersion = this.storage.getItem('appVersion')
    if (token) {
      this.login = true
    }
    this.appVersion = appVersion || '1.0.0'
  }
  // 返回
  onClickLeft() {
    this.location.back()
  }
  // 退出登录
  logout() {
    Modal.alert('提示', '是否确定要退出登录？', [
      {
        text: '取消',
        onPress: () => {}
      },
      {
        text: '确定',
        onPress: () =>
          new Promise(resolve => {
            this.storage.removeItem('token')
            this.storage.removeItem('userinfo')
            this.router.navigate(['/workspace/home/index'])
            this.utils.success('已退出登录')
            resolve()
          })
      }
    ])
  }
}
